package com.erdos.hyt.mall.controller;/**
 * @auth Liurong
 * @date 2017/3/16.
 */

import com.erdos.hyt.base.model.HytResponse;
import com.erdos.hyt.base.model.ResponseResultHandler;
import com.erdos.hyt.base.tools.aliyun.oss.OSSFilePath;
import com.erdos.hyt.base.tools.aliyun.oss.SimpleUpload;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用controller（上传图片等）
 *
 * @author liurong
 * @create 2017/3/16
 **/
/*@RestController
@RequestMapping(value = "/mall")
@ResponseBody*/
public class CommenController {

    private static Logger logger = LoggerFactory.getLogger(CommenController.class);
  /*  @ApiOperation(value = "上传图片", notes="平台上传图片通用接口", response = HytResponse.class)
   @RequestMapping(value = "/uploadPic",method = {RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
    public HytResponse saveImg(
            @ApiParam(value = "etc标题",required = true)@RequestParam(value = "etctitle")
            MultipartFile etctitle,
            @ApiParam(value = "etc01",required = true)@RequestParam(value = "etc01")
            MultipartFile etc01,
            @ApiParam(value = "etc02",required = true)@RequestParam(value = "etc02")
            MultipartFile etc02,
            @ApiParam(value = "kunluntitle",required = true)@RequestParam(value = "kunluntitle")
            MultipartFile kunluntitle,
            @ApiParam(value = "kunlun01",required = true)@RequestParam(value = "kunlun01")
            MultipartFile kunlun01,
            @ApiParam(value = "kunlun02",required = true)@RequestParam(value = "kunlun02")
            MultipartFile kunlun02
            )throws  Exception{
        HytResponse hytResponse =null;



        List<String> paths=new ArrayList<String>();
        paths.add(savePic(1,etctitle,"etc_title"));
        paths.add(savePic(1,etc01,"etc_01"));
        paths.add(savePic(1,etc02,"etc_02"));
        paths.add(savePic(2,kunluntitle,"kunlun_title"));
        paths.add(savePic(2,kunlun01,"kunlun_01"));
        paths.add(savePic(2,kunlun02,"kunlun_02"));
        hytResponse=ResponseResultHandler.handleSuccess(paths);

        return hytResponse;
    }*/

    private  String savePic(Integer id,MultipartFile pic,String imgTag)throws  Exception{
        OSSFilePath ossFilePath=OSSFilePath.MALL_GOODS_IMG;
        String oName=pic.getOriginalFilename();
        String suffix=oName.substring(oName.lastIndexOf("."), oName.length());
        String name= "goods_"+id+"_"+imgTag+suffix;//固定名称，便于后期修改覆盖
        InputStream is=pic.getInputStream();
        SimpleUpload simpleUpload = new SimpleUpload();
        simpleUpload.upload(name, ossFilePath, is);
        String s=ossFilePath.getFilePath()+"/"+name;
        System.out.print("文件名："+s);
        return  s;
    }
}
